﻿using Rabbit.Domain;
using Rabbit.Domain.Processing;
using Rabbit.Domain.Slaughter;
using Rabbit.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace Rabbit.Service
{
    public class ProcessingService
    {
        /// <summary>
        /// 深加工信息
        /// </summary>
        RabbitRepository<ProcessingInfo> _ires;
        /// <summary>
        /// 深加工图片
        /// </summary>
        RabbitRepository<ProcessingImg> _imgres;
        /// <summary>
        /// 深加工企业信息
        /// </summary>
        RabbitRepository<ProcessingQY> _qyres;
        /// <summary>
        /// 屠宰信息
        /// </summary>
        RabbitRepository<SlaughterInfo> _sires;
        /// <summary>
        /// 溯源管理
        /// </summary>
        RabbitRepository<TraceManage> _tres;

        public ProcessingService(RabbitRepository<ProcessingInfo> ires, RabbitRepository<ProcessingImg> imgres,
            RabbitRepository<ProcessingQY> qyres, RabbitRepository<SlaughterInfo> sires, RabbitRepository<TraceManage> tres)
        {
            _imgres = imgres;
            _qyres = qyres;
            _sires = sires;
            _tres = tres;
            _ires = ires;
        }


        #region 深加工信息

        /// <summary>
        /// 深加工信息
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<ProcessingInfo> GetProcessingInfo(int tmid)
        {
            return _ires.GetAll().Where(x => x.TMid == tmid).ToList();
        }
        /// <summary>
        /// 添加或修改深加工信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddProcessingInfo(ProcessingInfo input)
        {
            var list = _ires.GetAll().FirstOrDefault(x => x.TMid == input.TMid);
            if (list != null)
            {
                //修改
                list.PITeam = input.PITeam;
                list.PIPeople = input.PIPeople;
                list.PIid = input.PIid;
                list.TMid = input.TMid;
                _ires.Updates(list);
                return 1;//修改成功
            }
            _ires.Add(input);
            return 2;//添加成功
        }





        #endregion


        #region 食品安全信息

        /// <summary>
        /// 添加食品安全信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddProcessingImg(ProcessingImg input)
        {
            var list = new ProcessingImg();
            list.AddDate = DateTime.Now;
            list.AddName = input.AddName;
            list.PImgurl = input.PImgurl;
            list.TMid = input.TMid;
            list.PImgstate = 1;
            return _imgres.Add(list);
        }
        /// <summary>
        /// 食品安全信息
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<ProcessingImg> GetProcessingImg(int tmid)
        {
            var data = _imgres.GetAll().Where(x => x.TMid == tmid && x.IsDel != 1 && x.PImgstate == 1).ToList();
            return data;
        }
        /// <summary>
        /// 删除食品安全信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelProcessingImg(int ids)
        {
            var list = _imgres.GetAll().Where(x => x.PImgId == ids).ToList();
            foreach (var item in list)
            {
                item.IsDel = 1;
                item.DelDate = DateTime.Now;
                item.DelName = "老八";
            }
            return _imgres.Update(list);
        }

        #endregion


        #region 供应商信息

        /// <summary>
        /// 修改批次
        /// </summary>
        /// <param name="pc"></param>
        /// <param name="tMid"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public int Updpc(string pc,int tMid)
        {
            try
            {
                using (TransactionScope scop=new TransactionScope())
                {
                    //修改屠宰信息里面批次的信息
                    var list = _sires.GetAll().FirstOrDefault(x => x.TMid == tMid);
                    list.TMBatch = pc;
                    var lists= _sires.Updates(list);
                    var lis = _tres.GetAll().FirstOrDefault(x => x.TMid == tMid);
                    lis.TMBatch = pc;
                    var liss= _tres.Updates(lis);
                    if (lists > 0 && liss > 0)
                    {
                        scop.Complete();//提交事务
                        return 1;
                    }
                    return 0;
                }
            }
            catch (Exception)
            {

                throw new Exception("操作失败");
            }
            
        }

        #region 原料相关材料

        /// <summary>
        /// 添加原料相关材料
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddProcessingImg2(ProcessingImg input)
        {
            var list = new ProcessingImg();
            list.AddDate = DateTime.Now;
            list.AddName = input.AddName;
            list.PImgurl = input.PImgurl;
            list.TMid = input.TMid;
            list.PImgstate = 2;
            return _imgres.Add(list);
        }
        /// <summary>
        /// 原料相关材料
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<ProcessingImg> GetProcessingImg2(int tmid)
        {
            var data = _imgres.GetAll().Where(x => x.TMid == tmid && x.IsDel != 1 && x.PImgstate == 2).ToList();
            return data;
        }

        #endregion


        #region 辅料相关材料

        /// <summary>
        /// 添加辅料相关材料
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddProcessingImg3(ProcessingImg input)
        {
            var list = new ProcessingImg();
            list.AddDate = DateTime.Now;
            list.AddName = input.AddName;
            list.PImgurl = input.PImgurl;
            list.TMid = input.TMid;
            list.PImgstate = 3;
            return _imgres.Add(list);
        }
        /// <summary>
        /// 原料相关材料
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<ProcessingImg> GetProcessingImg3(int tmid)
        {
            var data = _imgres.GetAll().Where(x => x.TMid == tmid && x.IsDel != 1 && x.PImgstate == 3).ToList();
            return data;
        }

        #endregion


        #region 添加剂相关材料

        /// <summary>
        /// 添加添加剂相关材料
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddProcessingImg4(ProcessingImg input)
        {
            var list = new ProcessingImg();
            list.AddDate = DateTime.Now;
            list.AddName = input.AddName;
            list.PImgurl = input.PImgurl;
            list.TMid = input.TMid;
            list.PImgstate = 4;
            return _imgres.Add(list);
        }
        /// <summary>
        /// 添加剂相关材料
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<ProcessingImg> GetProcessingImg4(int tmid)
        {
            var data = _imgres.GetAll().Where(x => x.TMid == tmid && x.IsDel != 1 && x.PImgstate == 4).ToList();
            return data;
        }

        #endregion


        #region 包材相关材料

        /// <summary>
        /// 包材加剂相关材料
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddProcessingImg5(ProcessingImg input)
        {
            var list = new ProcessingImg();
            list.AddDate = DateTime.Now;
            list.AddName = input.AddName;
            list.PImgurl = input.PImgurl;
            list.TMid = input.TMid;
            list.PImgstate = 5;
            return _imgres.Add(list);
        }
        /// <summary>
        /// 包材相关材料
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<ProcessingImg> GetProcessingImg5(int tmid)
        {
            var data = _imgres.GetAll().Where(x => x.TMid == tmid && x.IsDel != 1 && x.PImgstate == 5).ToList();
            return data;
        }

        #endregion




        #endregion


        #region 深加工企业信息

        /// <summary>
        /// 深加工企业信息
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<ProcessingQY> GetProcessingQY(int tmid)
        {
            return _qyres.GetAll().Where(x => x.TMid == tmid).ToList();
        }
        /// <summary>
        /// 添加或修改深加工企业信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddProcessingQY(ProcessingQY input)
        {
            var list = _qyres.GetAll().FirstOrDefault(x => x.TMid == input.TMid);
            if (list != null)
            {
                //修改
                list.PQYid = input.PQYid;
                list.PQYName = input.PQYName;
                list.TMProduction = input.TMProduction;
                list.TMid = input.TMid;
                _qyres.Updates(list);
                var lis = _tres.GetAll().FirstOrDefault(x => x.TMid == input.TMid);
                if (lis != null)
                {
                    if (input.TMProduction != lis.TMProduction)
                    {
                        lis.TMProduction = input.TMProduction;
                        _tres.Updates(lis);
                    }
                }
                return 1;//修改成功
            }
            _qyres.Add(input);
            return 2;//添加成功
        }





        #endregion



        #region 企业照片

        /// <summary>
        /// 添加企业照片
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public int AddProcessingImg6(ProcessingImg input)
        {
            var list = new ProcessingImg();
            list.AddDate = DateTime.Now;
            list.AddName = input.AddName;
            list.PImgurl = input.PImgurl;
            list.TMid = input.TMid;
            list.PImgstate = 6;
            return _imgres.Add(list);
        }
        /// <summary>
        /// 企业照片
        /// </summary>
        /// <param name="tmid"></param>
        /// <returns></returns>
        public List<ProcessingImg> GetProcessingImg6(int tmid)
        {
            var data = _imgres.GetAll().Where(x => x.TMid == tmid && x.IsDel != 1 && x.PImgstate == 6).ToList();
            return data;
        }

        #endregion





    }
}
